<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="../lib/underscore.js"></script>
</head>
<body>

    <script>
    var arr = [1, 2, 3, 4, 5, 6, 0];
    var obj = {name: 'aaa', age: 20, gender: '男', email: 'aaa@126.com'};
    var arr2 = [
        {name: 'aaa', age: 20, gender: '女'},
        {name: 'bbb', age: 21, gender: '女'},
        {name: 'ccc', age: 20, gender: '男'},
        {name: 'ddd', age: 13, gender: '女'},
        {name: 'eee', age: 29, gender: '男'},
        {name: 'fff', age: 28, gender: '女'},
        {name: 'ggg', age: 15, gender: '男'},
        {name: 'hhh', age: 25, gender: '女'}
    ];

    var r = _.countBy(arr2, function(obj) {return obj.age >= 18 ? "成年" : "未成年";});
    console.log(r);

    /*var r = _.indexBy(arr2, 'age');
    console.log(r);*/

    /*var r = _.sortBy(arr2, 'age');
    console.log(r);*/

    /*var max = function(arr) {

        arr.sort(function(o1, o2) {
            return o2.age - o1.age;
        });

        return arr[0];

        // var i, len = arr.length, age = 0, obj;
        // for (i=0; i<len; i++) {
        //     if (arr[i].age > age) {
        //         age = arr[i].age;
        //         obj = arr[i];
        //     }
        // }
        // return obj;
    };

    var r = max(arr2);
    console.log(r);*/

    /*var r = _.min(arr2, function(user) {return user.age;});
    console.log(r);*/

    /*var r = _.pluck(arr2, 'name');
    console.log(r);*/

    /*var r = _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
    console.log(r)*/

    /*var r = _.contains(arr, 7);
    console.log(r);*/

    /*var some = function(arr) {
        var i, len = arr.length;
        var flag = false;
        for (i=0; i<len; i++) {
            if (arr[i] > 8) {
                flag = true;
            }
        }
        return flag;
    };

    var r = some(arr);
    console.log(r);*/

    /*var every = function(arr) {
        var i, len = arr.length;
        var flag = true;
        for (i=0; i<len; i++) {
            if (arr[i] <= 0) {
                flag = false;
            }
        }
        return flag;
    };

    var r = every(arr);
    console.log(r);*/

    /*var r = _.some(arr, function(n) {return n>3});

    console.log(r);*/

    /*var r = _.findWhere(arr2, {age: 20, gender: '男'});

    console.log(r);*/

    /*var r = _.filter(arr2, function(obj) {return obj.age % 2 == 0});
    console.log(r)*/

    /*var filter = function(arr, fun) {
        return fun(arr);
    };

    var myFun = function(arr) {
        var i, len = arr.length;
        var evenArr = [];
        for (i=0; i<len; i++) {
            if (arr[i] % 2 ==0) {
                evenArr.push(arr[i]);
            }
        }
        return evenArr;
    };

    var myFun2 = function() {
        var i, len = arr.length;
        var evenArr = [];
        for (i=0; i<len; i++) {
            if (arr[i] > 3) {
                evenArr.push(arr[i]);
            }
        }
        return evenArr;
    };

    var r = filter(arr, myFun);
    console.log(r)

    var r2 = filter(arr, myFun2);
    console.log(r2)*/

    /*var filter = function(tmpArr) {
        var i, len = tmpArr.length;
        var evenArr = [];
        for (i=0; i<len; i++) {
            if (tmpArr[i] > 3) {
                evenArr.push(tmpArr[i]);
            }
        }
        return evenArr;
    };

    var r = filter(arr);
    console.log(r);*/

    // var r = _.filter(arr, function(n) {return n % 2 == 0;});
    // console.log(r)

    /*function find(arr) {
        var i, len = arr.length;
        for (i=0; i<len; i++) {
            if (arr[i] % 2 == 0) {
                return arr[i];
            }
        }
        return undefined;
    }

    var r = find(arr);
    console.log(r)*/

    // var arr2 = _.map(arr, function(num) { return num * 2; });
    
    // var opts = _.map(arr, function(num) { return '<option>' + num + '</option>'} ); 
    
    // var opts = _.map(obj, function(v, k) { return '<option>' + k + '</option>'});
    
    // var r = _.reduce(arr, function(m, v) { return m + v;}, 5);

    // console.log(r);

    /*_.each(obj, function(val, index) {
        console.log(index)
    });*/

    /*var r = _.find(arr, function(n) {console.log(n); return n % 2 == 0});
    console.log(r);*/


    </script>
    
</body>
</html>